package Client.GUI;
/**
 * 
 * /**
 * 
 * @author Baglama Team
 *
 *
 */
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JOptionPane;

import Client.logic.ClientIF;
import Server.DataBase.Course;
import Server.DataBase.SchoolClass;
import Server.DataBase.Student;
import Server.DataBase.Teacher;
import Server.Message.MessageAddLesson;
import Server.Message.MessageGetAllCourses;
import Server.Message.MessageGetAllSchoolClass;
import Server.Message.MessageGetAllSchoolClassReplay;
import Server.Message.MessageGetClassByCourse;
import Server.Message.MessageGetCoursesReplay;
import Server.Message.MessageGetStudentsReplay;
import Server.Message.MessageTeacherByUnit;
import Server.Message.MessageTeacherReplay;
/**
 *  secratery initializes AssignClassToCourse panel
 * */
public class AssignClassToCoursePanel extends MyJPanel {
	/**
	 * /**
	 //private static final long serialVersionUID = 1L;
	 */ 
	private static final long serialVersionUID = 1L;
	 /**
	 //private static final long serialVersionUID = 1L;
	 */
	ArrayList<Course> arrayCourse;
	ArrayList<SchoolClass> arrayClass;
	ArrayList<Teacher> arrayTeacher;
	JComboBox comboBoxCourse, comboBoxClass, comboBoxTeacher;
	String selectCourse, selectClass, selectTeacher;
	JLabel lblChooseClass, lblCourse, lblTeacher;
	JButton btnFinish, btnReset, btnAssign;
	Course course;
	SchoolClass sClass;
	Teacher teacher;
	
	
	
	public AssignClassToCoursePanel(ClientIF client) {
		super(PanelType.ASSIGN_CLASS_TO_COURSE_PANEL, client);
		setLayout(null);
		BufferedImage myPic;
		try {
			myPic = ImageIO.read(new File("images"+ File.separator+"assign class to course.jpg"));
		
			JLabel lblAddClass = new JLabel(new ImageIcon(myPic), JLabel.CENTER);
			lblAddClass.setBounds(12, 13, 511, 99);
			add(lblAddClass);
		
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		init();
	}
	
	public void initLabels(){
		
		lblChooseClass = new JLabel("choose class:");
		lblChooseClass.setBounds(152, 186, 100, 14);
		add(lblChooseClass);
		
		lblCourse = new JLabel("course:");
		lblCourse.setBounds(152, 142, 46, 14);
		add(lblCourse);
		
		lblTeacher = new JLabel("teacher:");
		lblTeacher.setBounds(150, 234, 73, 14);
		add(lblTeacher);
	}
	
	public void initButtons(){
		btnFinish = new JButton("Finish");
		btnFinish.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Object[] options = {"Continue",
	                    "Cancel"};
				int n = JOptionPane.showOptionDialog((Component) getClient(),
						"After clicking ok you will not be able to assign more classes to courses\n"
								+ "at current semester are you sure you want to continue? ",
								"Alert",
								JOptionPane.YES_NO_OPTION,
								JOptionPane.WARNING_MESSAGE,
								null,
								options,
								options[1]);
				if (n==0) {
					getClient().exitProtectMode();
					getClient().swapFromBack(new HomePanelSecratery(getClient()));
				}
			}
				
			});
		btnFinish.setBounds(212, 369, 118, 23);
		add(btnFinish);
		
		btnReset = new JButton("Reset");
		btnReset.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().swapFromBack(pushPanel());
			}
		});
		btnReset.setBounds(273, 284, 115, 23);
		btnReset.setEnabled(false);
		add(btnReset);
		
		btnAssign = new JButton("assign");
		btnAssign.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				getClient().sendMsgToServer(new MessageAddLesson(course.getIdCourse(), sClass.getIdClass(), teacher.getIdUser()));
				MessageGetStudentsReplay msg = (MessageGetStudentsReplay) getClient().getMessageFromServer();
				String str;
				if (msg.getArray().size()>0){
					str="The following student don't meet the precondition:\n";
					for (Student student: msg.getArray())
						str+=student.getLastName()+" "+student.getFirstName()+"\n";
				}
				else{
					str = "All students were assign successfully";
				}
				JOptionPane.showMessageDialog((Component) getClient(), str);
				getClient().swapFromBack(pushPanel());
			}
		});
		btnAssign.setEnabled(false);
		btnAssign.setBounds(152, 284, 114, 23);
		add(btnAssign);
	}
	
	public void initComboBoxs(){
		comboBoxCourse = new JComboBox();
		comboBoxCourse.setBounds(262, 139, 126, 20);
		add(comboBoxCourse);
		
		comboBoxClass = new JComboBox();
		comboBoxClass.setBounds(262, 183, 126, 20);
		comboBoxClass.setEnabled(false);
		add(comboBoxClass);
		
		comboBoxTeacher = new JComboBox();
		comboBoxTeacher.setBounds(262, 231, 126, 20);
		comboBoxTeacher.setEnabled(false);
		add(comboBoxTeacher);
	}
	
	public void init(){
		
		initButtons();
		initComboBoxs();
		initLabels();
		getClient().sendMsgToServer(new MessageGetAllCourses());
		MessageGetCoursesReplay msg = (MessageGetCoursesReplay) getClient().getMessageFromServer();
		arrayCourse = msg.getCourseArray();
		comboBoxCourse.addItem("Choose..");
		for (Course course : arrayCourse)
			comboBoxCourse.addItem(course);
		comboBoxCourse.addActionListener(new CourseListener());
				
	}
	
	private class CourseListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				course =  (Course) comboBoxCourse.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageGetAllSchoolClass());
				MessageGetAllSchoolClassReplay msg2 = (MessageGetAllSchoolClassReplay) getClient().getMessageFromServer();
				arrayClass = msg2.getArray();
			
				getClient().sendMsgToServer(new MessageGetClassByCourse(course.getIdCourse()));
				MessageGetAllSchoolClassReplay msg3 = (MessageGetAllSchoolClassReplay) getClient().getMessageFromServer();
				ArrayList<SchoolClass> arrayClass2 = msg3.getArray();
				boolean next= true;
				int i;
				for (SchoolClass sClass1: arrayClass2){
					next= true;
					i=0;
					while (next && i<arrayClass.size()){
						if (arrayClass.get(i).getIdClass() == sClass1.getIdClass()){
							arrayClass.remove(i);
							next=false;
						}
						else i++;
					}
				}
			
				comboBoxClass.addItem("Choose..");
				for (SchoolClass sClass3: arrayClass)
					comboBoxClass.addItem(sClass3);
				comboBoxClass.setEnabled(true);
				comboBoxCourse.setEnabled(false);
				btnReset.setEnabled(true);
				comboBoxClass.addActionListener(new ClassListener());
			}catch (java.lang.Throwable t){}
		}
	}
	
	private class ClassListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				sClass = (SchoolClass) comboBoxClass.getSelectedItem();
			
				getClient().sendMsgToServer(new MessageTeacherByUnit(course.getTunit()));
				MessageTeacherReplay msg = (MessageTeacherReplay) getClient().getMessageFromServer();
				arrayTeacher = msg.getArray();
			
				comboBoxTeacher.addItem("Choose..");
				for (Teacher teacher : arrayTeacher){
					if (teacher.getMaxHours()>=(teacher.getHours()+course.getHours()))
					comboBoxTeacher.addItem(teacher);
				}
				comboBoxClass.setEnabled(false);
				comboBoxTeacher.addActionListener(new TeacherListener());
				comboBoxTeacher.setEnabled(true);
			} catch (java.lang.Throwable t){}
		}
	}

	private class TeacherListener implements ActionListener{
		public void actionPerformed(ActionEvent event) {
			try{
				teacher = (Teacher) comboBoxTeacher.getSelectedItem();
				btnAssign.setEnabled(true);
			}catch (java.lang.Throwable t){}
		}
	}

	@Override
	public MyJPanel pushPanel() {
		return new AssignClassToCoursePanel(getClient());
	}
}
